Skip to content

flatMap reads the array its callback gives and leaves it as it was - #99

Merged
ASDAlexander77 merged 1 commit into
mainfrom
flatmap-keeps-callback-array
Nov 14, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
flatmap-keeps-callback-array

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

What

flatMap no longer empties the array its callback returns. It copies the elements out, as flat does, where it moved them.

Why

A callback that returned an array it kept had it drained on the first call: the same array for every element, or a row of a table.

const pair: number[] = [7, 8];
[1, 2, 3].flatMap((v: number, i: number) => pair);  // was "7,8"; now "7,8,7,8,7,8"
// pair was left empty; now it is still [7, 8]

Change

  • src/generics/lib.generics.ts: the loop in __Array.flatMap pushes each element of the returned array. Array<T>.flatMap calls it, so it is fixed too.
  • tests/array_flatMap_shared.ts (new): a shared array, rows of a table, and the Array<T> class. It failed before the fix.

For the reviewer

Testing

  • Release, Windows x64: the whole suite passes for gc, rc and none, compiled and under the JIT, and for own under the JIT; the two flat tests were also run compiled for own.
  • Not run locally: debug builds, Linux, x86 and wasm.

🤖 Generated with Claude Code

flatMap moved the elements out of the array the callback returned, which
emptied one the callback did not make: the same array for every element, or a
row of a table. [1, 2, 3].flatMap(v => pair) gave pair's elements once and
left pair empty. It copies them now, as flat does.

array_flatMap_shared.ts covers it on T[] and on Array<T>. It is marked
"// not own": -mm=own refuses a callback that returns an array it does not
own, so the case does not arise there.

Tested in release on Windows x64 for gc, rc, none and own; debug, Linux, x86
and wasm were not built locally.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 0fac086 into main Nov 14, 2026
10 checks passed
@ASDAlexander77
ASDAlexander77 deleted the flatmap-keeps-callback-array branch November 14, 2026 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant